<?php namespace Admin\Api;


class Set{
	
	
	protected $error = '未知错误';
	
	protected $db;
	
	public function __construct()
	{
		$this->db = new \Admin\Model\Set;
	}
	
	public function getToken()
	{
		$token = substr(md5($_SESSION['id']), 0,6);
		return $token;
	}
	
	
	public function save()
	{
		$set = Db::table('set')->where('username',$_SESSION['username'])->first();
		$_POST['username'] = $_SESSION['username'];
		if($set)
		{
			$_POST['id'] = $set['id'];
			if($this->db->create())
			{
				if($this->db->save())
				{
					return array('code'=>0,'message'=>'更新操作成功');
				}
			}
		}
		else{
			$_POST['token'] = $this->getToken();
			if($this->db->create())
			{
				if($this->db->add())
				{
					return array('code'=>0,'message'=>'添加操作成功');
				}
			}
		}
		
		
		return array('code'=>1,'message'=>$this->getError());
	}
	
	
	public function getError() {
		return $this->db->getError() ? : $this->error;
	}
	
	
	
	
	
	
	
}




















